package com.nageoffer.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nageoffer.shortlink.common.constant.RedisConstant;
import com.nageoffer.shortlink.common.convention.result.Result;
import com.nageoffer.shortlink.common.dto.project.req.ShortLinkMoveToBinReqDTO;
import com.nageoffer.shortlink.common.dto.project.resp.ShortLinkPageBinRespDTO;
import com.nageoffer.shortlink.common.entity.admin.Group;
import com.nageoffer.shortlink.project.dao.entity.ShortLink;
import com.nageoffer.shortlink.project.dao.mapper.ShortLinkMapper;
import com.nageoffer.shortlink.project.dto.req.ShortLinkPageBinReqDTO;
import com.nageoffer.shortlink.project.service.RecycleBinService;
import com.nageoffer.shortlink.remote.GroupClient;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLink> implements RecycleBinService {
    private final StringRedisTemplate stringRedisTemplate;
    private final GroupClient groupClient;
    private final ShortLinkMapper shortLinkMapper;
    @Override
    public void moveToRecycleBin(ShortLinkMoveToBinReqDTO reqParam) {
        LambdaUpdateWrapper<ShortLink> wrapper = Wrappers.lambdaUpdate(ShortLink.class)
                .eq(ShortLink::getGid, reqParam.getGid())
                .eq(ShortLink::getFullShortUrl, reqParam.getFullShortUrl())
                .eq(ShortLink::getEnableStatus, 0)
                .eq(ShortLink::getDelFlag, 0);
        ShortLink shortLink = ShortLink.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(shortLink,wrapper);
        //删除缓存
        stringRedisTemplate.delete(RedisConstant.GOTO_SHORT_LINK_KEY+reqParam.getFullShortUrl());
    }

    @Override
    public List<ShortLinkPageBinRespDTO> pageRecycleBin(ShortLinkPageBinReqDTO reqParam) {
        Result<List<Group>> simpleGroups = groupClient.getSimpleGroups();
        List<String> ids = simpleGroups.getData().stream().map(Group::getGid).toList();
        LambdaQueryWrapper<ShortLink> eq = Wrappers.lambdaQuery(ShortLink.class)
                .eq(ShortLink::getEnableStatus, 1)
                .eq(ShortLink::getDelFlag, 0)
                .in(ShortLink::getGid, ids);
        IPage<ShortLink> shortLinkIPage = shortLinkMapper.selectPage(reqParam, eq);
        return shortLinkIPage.getRecords().stream()
                .map(each -> BeanUtil.toBean(each, ShortLinkPageBinRespDTO.class))
                .toList();
    }

    @Override
    public void backFromRecycleBin(ShortLinkMoveToBinReqDTO reqParam) {
        LambdaUpdateWrapper<ShortLink> wrapper = Wrappers.lambdaUpdate(ShortLink.class)
                .eq(ShortLink::getGid, reqParam.getGid())
                .eq(ShortLink::getFullShortUrl, reqParam.getFullShortUrl())
                .eq(ShortLink::getEnableStatus, 1)
                .eq(ShortLink::getDelFlag, 0);
        ShortLink shortLink = ShortLink.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(shortLink,wrapper);
        //删除缓存空值
        stringRedisTemplate.delete(RedisConstant.NULL_SHORT_LINK_KEY+reqParam.getFullShortUrl());
    }

    @Override
    public void delFromRecycleBin(ShortLinkMoveToBinReqDTO reqParam) {
        LambdaUpdateWrapper<ShortLink> wrapper = Wrappers.lambdaUpdate(ShortLink.class)
                .eq(ShortLink::getGid, reqParam.getGid())
                .eq(ShortLink::getFullShortUrl, reqParam.getFullShortUrl())
                .eq(ShortLink::getEnableStatus, 1)
                .eq(ShortLink::getDelFlag, 0);
        ShortLink shortLink = ShortLink.builder()
                .del_time(new Date())
                .build();
        shortLink.setDelFlag(1);
        baseMapper.update(shortLink,wrapper);
    }

}
